import Koa from 'koa';
import path from 'path';
import routers from './router/router'
import bodyParser from 'koa-bodyparser';
import judge from './util/judge'
import koaStatic from 'koa-static';
import isLogin from './middleware/isLogin'
import db from './db/index';

const { querys, insert } = db


// 服务
const app = new Koa();


// 在ctx上扩展一个 db 对象
app.context.db = {
  querys, insert
}
app.context.isJudge = judge

// 处理静态资源
app.use(koaStatic(path.join(__dirname, './public')));

// 解析客户发送的数据
app.use(bodyParser());


// 鉴权
app.use(isLogin());

// koa 路由
app.use(routers.routes());
app.use(routers.allowedMethods());
app.listen(8000, () => console.log(`server run 8000`));